package spring.cloud.tasks.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import spring.cloud.tasks.api.domain.NameSpace;
import spring.cloud.tasks.api.service.NameSpaceService;
import spring.cloud.tasks.api.service.ZooKeeperService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * RESTful APIs of namespace management.
 *
 * @author kfchu
 */
@RequestMapping("/api")
public class NamespaceManagementController {

    @Autowired
    private NameSpaceService namespaceService;
    @Autowired
    private ZooKeeperService zooKeeperService;

    //@Audit(type = AuditType.REST)
    @RequestMapping(value = "/createNameSpace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> createNameSpace(@RequestBody Map<String, Object> reqParams, HttpServletRequest request)
            throws RuntimeException {
        try {
            NameSpace nameSpace = new NameSpace();
            nameSpace.setNameSpace((String) reqParams.get("nameSpace"));
            nameSpace.setZooKeeperClusterKey((String) reqParams.get("zooKeeperClusterKey"));
            nameSpace.setContent("");
            zooKeeperService.createNameSpace(nameSpace);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    //@Audit(type = AuditType.REST)
    @RequestMapping(value = "/namespaces/{namespace:.+}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> deleteNamespace(@PathVariable("namespace") String namespace) throws RuntimeException {
        try {

            namespaceService.deleteNamespace(namespace);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @RequestMapping(value = "/namespaces/{namespace:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> getNameSpace(@PathVariable("namespace") String namespace) throws RuntimeException {
        HttpHeaders httpHeaders = new HttpHeaders();
        try {

            NameSpace namespaceDomainInfo = zooKeeperService.getNameSpace(namespace);
            return new ResponseEntity<Object>(namespaceDomainInfo, httpHeaders, HttpStatus.OK);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

}
